var count = 1;

var crtObj = null;

var isFirst = "yes";

var attatchMode = 1;//1为内容，2为备注
function planNodeClass(){
    this.name = "";
    this.ctt = "";
    this.note = "";
}

var planArr = new Array();

var json = "{\"planSteps\":[";

$(document).ready(function(){
    $("#browser").treeview();
    $('body').click(function(){
        $('#rightMenu').remove();
    });
    $('#nodeFinish').click(function(){
        nodeFinish();
    });
    $(".pointerSP").bind("contextmenu", function(e){
        rightClick(e, $(this));
        return false;
    });
    $('#startFillStep').click(beginFillStep);
    ///
    $("#dialog").dialog("destroy");
    $("#dialog-form").dialog({
        autoOpen: false,
        width: 430,
        modal: true,
        resizable: false,
        buttons: {
            '确定': function(){
                $(this).dialog('close');
                //btnSure();
                insertNodeConduct();
            },
            '取消': function(){
                $(this).dialog('close');
            }
        },
        close: function(){
            $("#user-form input").removeClass('invalid');
            $("#user-form label").remove('.invalid');
            $("dialog").dialog("close");
        }
    });
});

function nodeFinish(){
    //var gou="{'planSteps':[{'stepName':'第1步a','stepCnt':'内容b','stepNote':'备注c'},{'stepName':'第2步a','stepCnt':'内容b','stepNote':'备注c']}";
    //eval('('+gou+')');
    
    //var gee="{'planSteps':[{'stepName':第1步dfg,'stepCnt':内容fdgdfg,'stepNote':备注fdgfdg},{'stepName':第2步dfg4,'stepCnt':内容fdgdfg4,'stepNote':备注fdgfdg4]}";
    //eval('('+"{'planSteps':[{'stepName':第1步dfg,'stepCnt':内容fdgdfg,'stepNote':备注fdgfdg},{'stepName':第2步dfg4,'stepCnt':内容fdgdfg4,'stepNote':备注fdgfdg4]}"+')');
    //eval(gee);
    //eval('('+gee+')');
    //alert($('#browser').html());
    //alert($('#browser ul').html());
    
    bianli($('#browser'), 0);
    json += "}]}";
    savePlanSteps(json);
    //alert(json);
    //var goodObj=eval('('+json+')');
    //alert(goodObj);
    //document.write(json);
    json = null;
    json = "{\"planSteps\":[";
    //document.write(json);
    //var goodObj=eval('('+json+')');
    //alert(goodObj);

    //alert(json);
    //alert(eval(json));
    //alert('('+eval(json)+')');
    //json="{'planSteps':[";
}


function savePlanSteps(planSteps){
	//alert($('#planID').text());
    var url = 'lzm/SavePlan_savePlanSteps.action';
    var timestamp = Date.parse(new Date());
    var params = {
        'timestamp': timestamp,
        'planSteps': planSteps,
		'planID':$('#planID').text()
    };
    jQuery.post(url, params, savePlanStepsRst, 'json');
}

/**
 * 作增加预案回调函数
 */
function savePlanStepsRst(data){
    if (data.ajaxRst == SUCCESS) {
        operateSucc("/ag/lzm/save_plan.jsp");
    }
    else {
        alert("定义预案工作流失败，请重试！");
    }
}


function beginFillStep(){
    $("#leftFrame1", parent.document.body).contents().find("#optDetUl").html($("#browser").html());
    $("#leftFrame1", parent.document.body).contents().find("#optDetUl").addClass("treeview-black");
    $("#leftFrame1", parent.document.body).contents().find("#optDetUl").treeview();
    window.location.href = "add_plan_fill_step.jsp";
}


function rightClick(e, itself){
    xOffset = 10;
    yOffset = 20;
    var str = "<p id='rightMenu'><img src='lzm/images/d.gif' width='17' height='18' /><a class='add'>增加操作</a>"
    str += "<br /><img src='lzm/images/c.gif' width='17' height='18' /><a  class='del'>删除操作</a></p>";
    $("body").append(str);
    $("#rightMenu").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px").fadeIn("slow");
    $('#rightMenu .add').unbind("click");
    $('#rightMenu .add').click(function(){
        //insertNodeNamePopup(itself);
        crtObj = itself;
        $('#dialog-form').dialog('open');
		$("#stepName", window.frames["addPlanNode"].document).val("");
		$("#stepCnt", window.frames["addPlanNode"].document).val("");
		$("#stepNote", window.frames["addPlanNode"].document).val("");
    });
    $('#rightMenu .del').click(function(){
        deleteNodeNamePopup();
    });
    //count++;
    //alert(count);
    //alert("dfgdf");
    //$('#dialog-form').dialog('open');
}

function deleteNodeNamePopup(){
    crtObj.parent().remove();
    $("#browser").treeview();
}

function insertNodeConduct(){
    /*
     if($('crtObj.parent()>ul').length > 0 ){
     alert("you");
     }*/
    var strHTML;
    if (crtObj.parent().children().length > 1) {
        strHTML = "<li><span class='pointerSP'>";
		/*
        strHTML += "第";
        strHTML += count++;
        strHTML += "步";
        */
        strHTML += $("#stepName", window.frames["addPlanNode"].document).val();
        strHTML += "</span><ul><li><span class='stepCnt'>";
        //strHTML += "内容";
        strHTML += $("#stepCnt", window.frames["addPlanNode"].document).val();
        strHTML += "</span></li>"
        strHTML += "<li><span class='stepNote'>";
        //strHTML += "备注";
        strHTML += $("#stepNote", window.frames["addPlanNode"].document).val();
        strHTML += "</span></li>"
        strHTML += "</ul></li>";
        var aaa = $(strHTML).appendTo(crtObj.next().treeview());
        crtObj.next().treeview().treeview({
            add: aaa
        });
    }
    else {
        strHTML = "<ul><li><span class='pointerSP'>"
		/*
        strHTML += "第";
        strHTML += count++;
        strHTML += "步";
        */
        strHTML += $("#stepName", window.frames["addPlanNode"].document).val();
        strHTML += "</span><ul><li><span class='stepCnt'>";
        //strHTML += "内容";
        strHTML += $("#stepCnt", window.frames["addPlanNode"].document).val();
        strHTML += "</span></li>";
        strHTML += "<li><span class='stepNote'>";
        //strHTML += "备注";
        strHTML += $("#stepNote", window.frames["addPlanNode"].document).val();
        strHTML += "</span></li>"
        strHTML += "</ul></li></ul>";
        var aaa = $(strHTML).appendTo(crtObj.parent().treeview());
        crtObj.parent().treeview().treeview({
            add: aaa
        });
    }
    /*	
     $("#browser").treeview({
     add: strHTML
     });*/
    $("#browser").treeview();
    $('body').click(function(){
        $('#rightMenu').remove();
    });
    
    /*
    
     $(".pointerSP").bind("contextmenu",function(e){
    
     rightClick(e,$(this));
    
     return false;
    
     });*/
    
    //alert($("#browser").html());
    /*
    
     $(crtObj.parent()).treeview({
    
     add: strHTML
    
     });
    
     * */
    
}

function bianli(aaa, layer){
    aaa.children().each(function(){
        var gg = layer;
        if ($(this).children().length == 0 && $(this).html().length != 0) {
            gg = ++layer;
            //alert($(this).html());
        }
        bianli($(this), gg);
        if ($(this).children().length == 0 && $(this).html().length != 0) {
            /*
             var step=new stepNode();
             step.nodeValue=$(this).html();
             step.lay=layer;
             step.jqueryObj=$(this);
             stepArr.push(step);*/
            //alert(layer);
            //alert($(this).text());
            
            if (layer == 2 && isFirst == "yes") {
                isFirst = "no";
                json += "{\"stepName\":";
                json += "\"";
                json += $(this).text();
                json += "\"";
            }
            else 
                if (layer == 2 && isFirst != "yes") {
                    json += "},{";
                    json += "\"stepName\":";
                    json += "\"";
                    json += $(this).text();
                    json += "\"";
                }
            if (layer == 3 && attatchMode == 1) {
                attatchMode = 2;
                json += ",";
                json += "\"stepCnt\":";
                json += "\"";
                json += $(this).text();
                json += "\"";
            }
            else 
                if (layer == 3 && attatchMode != 1) {
                    attatchMode = 1;
                    json += ",";
                    json += "\"stepNote\":";
                    json += "\"";
                    json += $(this).text();
                    json += "\"";
                }
        }
    });
}
